<?php


namespace App\Models;


use Illuminate\Support\Facades\DB;

class ImFilesStorage extends BaseModel
{
    protected $table = 'im_files_storage';

    //用户信息关联
    public function user()
    {
        return $this->belongsTo(ImUsers::class, 'user_id', 'id')
            ->select(['id', 'nickname as username', 'headimg']);
    }

    //后台账号关联
    public function account()
    {
        return $this->belongsTo(ImAccount::class, 'user_id', 'id')
            ->select(['id', 'username',DB::raw('"" as headimg')]);
    }

    /**
     * @desc 上传文件
     * @param int $userId 用户id
     * @return array|int[]
     */
    public function addFilesStorage($type, $fileId, $source, $linkUrl, $userId,$taskId,$vendor,$videoId,$power)
    {
        $this->type = $type;
        $this->file_id = $fileId;
        $this->source = $source;
        $this->user_id = $userId;
        $this->status = 1;//0禁用 1正常
        $this->link_url = $linkUrl;
        $this->task_id = $taskId;
        $this->vendor = $vendor;
        $this->video_id = $videoId;
        $this->power = $power;
        $flag = $this->save();
        if (!$flag) {
            return ['code' => 1];
        } else {
            return true;
        }
    }

}
